package org.jvending.provisioning.stocking.handler;

import org.hibernate.Hibernate;
import org.jvending.provisioning.model.clientbundle.ContentFile;
import org.jvending.provisioning.stocking.StockingException;
import org.jvending.provisioning.stocking.par.DescriptorFileType;
import org.jvending.registry.RepositoryRegistry;

import java.io.ByteArrayInputStream;
import java.sql.Blob;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class MavenHandler implements DescriptorHandler {

    /**
     * RepositoryRegisty instance
     */
    private RepositoryRegistry repositoryRegistry;

    private static Logger logger = Logger.getLogger("MavenHandler");

    public void setRepositoryRegistry(RepositoryRegistry repositoryRegistry) {
        this.repositoryRegistry = repositoryRegistry;
    }

    public List<ContentFile> getContentFiles(DescriptorFileType descriptorFileType, Map contentMap) throws StockingException {
        /*
        logger.info("Starting Maven Handler");
        List<ContentFile> contents = new ArrayList<ContentFile>();
        String uri = descriptorFileType.getValue();
        if (uri == null || isRemoteUri(uri.trim())) {
            logger.info("JV-1800-002: Remote or null URI. Not processing content: URI = " + uri);
            return contents;
        }
        String mimeType = descriptorFileType.getMimeType();
        MimeTypeRepository mimeRepo = (MimeTypeRepository) repositoryRegistry.find("mimetype");
        if (mimeType == null) mimeType = mimeRepo.getMimeTypeFromUri(descriptorFileType.getValue());

        if (mimeType == null) {
            logger.info("JV-1800-003: Cannot determine the mime-type of this descriptor: URI = " + uri);
            return contents;
        }

        if (!mimeType.equals("text/x-maven-descriptor")) {
            logger.info("JV-1800-004: Not Maven Descriptor mime-type: URI = " + uri + ", mimeType = " + mimeType);
            return contents;
        }

        byte[] descriptorBytes = (byte[]) contentMap.get(uri.trim());
        if (descriptorBytes == null) {
            logger.info("JV-1800-005: Cannot find local copy of this descriptor: URI = " + uri + ", Content Keys = "
                    + contentMap.keySet());
            return contents;
        }
        MavenDescriptorType mavenDescriptorType;
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance("org.jvending.provisioning.descriptor.maven");
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            unmarshaller.setValidating(true);
            mavenDescriptorType = (MavenDescriptorType) unmarshaller.unmarshal(new ByteArrayInputStream(descriptorBytes));
        } catch (JAXBException e) {
            e.printStackTrace();
            throw new StockingException("JV-101-002: Critical - Problem with Schema! ");
        }
        ContentFile assemblyFile = createContentFileFor(mavenDescriptorType.getContentFile(),
                "application/x-ms-library", contentMap);
        if(assemblyFile != null) contents.add(assemblyFile);

        ContentFile projectFile = createContentFileFor(mavenDescriptorType.getProjectFile(),
                "text/x-maven-pom", contentMap);
        if(projectFile != null) contents.add(projectFile);
        AA
        return contents;
        */
        return null;
    }

    private ContentFile createContentFileFor(String uri, String mimeType, Map contentMap)
    {
        byte[] assemblyFile = (byte[]) contentMap.get(uri.trim());
        if(assemblyFile == null) {
            logger.info("JV-1800-007: Could not find local copy of assembly: URI = " + uri + ", Content Keys = "
                    + contentMap.keySet());
            return null;
        }
        ByteArrayInputStream is = new ByteArrayInputStream(assemblyFile);
        Blob contentBlob = Hibernate.createBlob(is, assemblyFile.length);
        ContentFile contentFile = new ContentFile();
        contentFile.setContent(contentBlob);
        contentFile.setFileUri(uri);
        contentFile.setMimeType(mimeType);
        contentFile.setBytes(assemblyFile);
        return contentFile;
    }

    private boolean isRemoteUri(String uri) {
        return (uri != null && (uri.startsWith("http://") || uri.startsWith("ftp://") || uri.startsWith("https://")));
    }
}
